var app = getApp()
Page({

  /**
  * 页面的初始数据
  */

  data: {
      count:0,
    currtab: 0,
    swipertab: [ { name: '待付款', index: 0 },{ name: '已完成', index: 1 },{ name: '已过期', index: 2 }, { name: '已取消', index: 3 }, { name: '已退款', index: 4 }],
  },

  /**
  * 生命周期函数--监听页面加载
  */
  onLoad: function (options) {
   // console.log(options);
    var id = options.id;
    if(id==null){
      id=0;
    }
    this.data.currtab = parseInt(id);
    this.setData({
      currtab: this.data.currtab,
      domain:app.globalData.domain,
      width:app.globalData.windowWidth,
    height:app.globalData.windowHeight,
    statusBarHeight:app.globalData.statusBarHeight,
    navHeight:app.globalData.navHeight,
    marginTop:app.globalData.navTitleTop
    })
    //console.log(this.data.currtab);
    
  },
  /**
  * 生命周期函数--监听页面初次渲染完成
  */
  onReady: function () {
    // 页面渲染完成
    this.getDeviceInfo()
    this.orderShow()
  },

  getDeviceInfo: function () {
    let that = this
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          deviceW: res.windowWidth,
          //deviceH: res.windowHeight
        })
      }
    })
  },
orderPay:function(e){
  //console.log(e);
var order_id=e.currentTarget.dataset.orderid;
wx.navigateTo({
  url: '../match/detail?order_id='+order_id,
})


},
orderPay2:function(e){
  //console.log(e);
  var that=this;
  var open=wx.getStorageSync('open');
  wx.request({
    url:app.globalData.domain+'/match/match/Pay',    
    data: {order_id: e.currentTarget.dataset.orderid, open_id: open.id, openid: open.mini_openid,type:1 },
    success: function (res) {
     console.log(res.data);
      //console.log('调起支付');
      var code=res.data.code;
      if(code==200){
          var data=res.data.data;
        wx.requestPayment({
            'timeStamp': data.timeStamp,
            'nonceStr': data.nonceStr,
            'package': data.package,
            'signType': 'MD5',
            'paySign': data.paySign,
            'success': function (res) {
             // console.log('success');
              wx.showToast({
                title: '支付成功',
                icon: 'success',
                duration: 3000
              });
              that.orderShow();
            },
            'fail': function (res) {
              //console.log(res);
            },
            'complete': function (res) {
             // console.log('complete');
            }
          });
      }else{

      }
  
    },
    fail: function (res) {
      //console.log("fail")
    }
  }); 

},
orderCancel(e){
  console.log(e);
  var datas=e.currentTarget.dataset;
  var id=datas.id;
  var open=wx.getStorageSync('open');
  var data=this.data;
  let that=this;
  wx.request({
    url:app.globalData.domain+'/match/match/orderCancel',
    data:{id:id,open_id:open.id},
    success:function(res){
      //console.log(res);
      if(res.data.code==200){
          that.orderShow()
      }
    }
  })
},
orderRefund(e){
  console.log('----refund---')
  console.log(e);
  var datas=e.currentTarget.dataset;
  var id=datas.id;
  var transaction_id=datas.transaction_id;
  var open=wx.getStorageSync('open');
  var data=this.data;
  let that=this;
  wx.request({
    url:app.globalData.domain+'/match/match/refund',
    data:{id:id,open_id:open.id,transaction_id:transaction_id,type:1},
    success:function(res){
      ;
      let data=res.data;
      console.log(data)
      if(data.code==200){
       app.showModals(data.desc,data.url,data.index);
          that.orderShow()
      }else{
       app.showModals(data.desc,data.url,data.index);
      }
   
      
    }
  })
},
goBack:function(){
    wx.navigateBack();
  },
  /**
  * @Explain：选项卡点击切换
  */
  tabSwitchs: function (e) {
    console.log(e)
    var that = this
    if (this.data.currtab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currtab: e.target.dataset.current
      })
     that.orderShow()
    }
  },

  tabChange: function (e) {
    this.setData({ currtab: e.detail.current })
   // this.orderShow()
  },

  orderShow: function () {
    let that = this
    var open=wx.getStorageSync('open');
    wx.request({
        url:app.globalData.domain+'/wechat/match/getOrder',
        data: { status:this.data.currtab,open_id:open.id},
        success: (res) => {
          //alreadyOrder: res.data       
          console.log(res.data);
          //that.alreadyOrder = res.data;
          var count = res.data.list.length;
          //console.log(count); 
         
          that.setData({
              count:count,
            list: res.data.list,
            deviceH: count * 202
          })
          //console.log(alreadyOrder)
        }
      })
  },

  orderDetail:function(e){
   // console.log(e)
    wx.navigateTo({
      url: '../match/detail?order_id='+e.currentTarget.dataset.orderid,
    })
  },
  gotoMatch:function(e){
    //console.log(e.currentTarget.dataset.matchid);
    wx.redirectTo({
      url: '../match/match/match_id=' + e.currentTarget.dataset.matchid,
    })
  },

  /**
  * 生命周期函数--监听页面显示
  */
  onShow: function () {

  },

  /**
  * 生命周期函数--监听页面隐藏
  */
  onHide: function () {

  },

  /**
  * 生命周期函数--监听页面卸载
  */
  onUnload: function () {

  },

  /**
  * 页面相关事件处理函数--监听用户下拉动作
  */
  onPullDownRefresh: function () {

  },

  /**
  * 页面上拉触底事件的处理函数
  */
  onReachBottom: function () {

  },

  /**
  * 用户点击右上角分享
  */
  onShareAppMessage: function () {

  }
})